/**
 * @class common.base.modal
 * @extends common.base.object
 * 弹层基类，不支持tab切换
 */
define(function (require) {
    require('slimscroll');
    require('jqui_draggable');
    require('jqui_resizable');
    var BaseObject = require('common/base/view'),
        Tpl = require('text!./templates/modal.html');
    var dlg_top = 50;

    return BaseObject.extend({
        initialize: function (params) {
            var _this = this;
            BaseObject.prototype.initialize.apply(this, [params]);
            //"#"+_this.id = "#" + this.id;
            _this.el="#" + this.id;
            _this.html = _this.render(Tpl, _.extend({
                index: window.Z_INDEX++,
                name: _this.name
            }, params));

            _this.params = params;
            _this.scrollable = _.isset(params.scrollable) ? params.scrollable : true;
            _this.scroll = _.isset(params.scroll) ? params.scroll : 'slimScroll';
            _this.resizeable = _.isset(params.resizeable) ? params.resizeable : true;
        },
        setContent: function (html) {
            var _this = this;
            _this.$('.modal-content').html(html);
            _this.$('.modal-content .loading').removeClass('loading');

            _this.$('.modal-dialog').draggable({
                handle: ".modal-header,.modal-footer"
            });
            _this.$('.close,.btn-cancel').click(function () {
                _this.close();
            });
            $("#"+_this.id).scroll(function () {
                _this.resize();
            });
            $(window).resize(function () {
                _this.resize();
            });
        },
        resize: function () {
            return;
            var _this = this,
                scrollTop = $("#"+_this.id).scrollTop(),
                window_height = $(window).height(),
                dialog_height = _this.$('.modal-dialog').height(),
                header_height = _this.$('.modal-header').height(),
                footer_height = _this.$('.modal-footer').height();
            if (scrollTop - dlg_top > 0) {
                _this.$('.modal-header').css({
                    'position': 'absolute',
                    'top': (scrollTop - dlg_top - 2) + 'px',
                    'width': _this.$('.modal-dialog').width() + 'px',
                    'z-index': 1000
                });
                _this.$('.pos_header').css({
                    'position': 'absolute',
                    'top': (scrollTop + _.intval(_this.$('.pos_header').data('offset'))) + 'px'
                });
            } else {
                _this.$('.modal-header').css({
                    'position': 'static'
                });
            }
            if (_this.$('.modal-dialog').height() - window_height - scrollTop - header_height > 0) {
                _this.$('.modal-footer').css({
                    'position': 'absolute',
                    'top': (scrollTop + window_height - _this.$('.modal-footer').height() - 68) + 'px',
                    'width': _this.$('.modal-dialog').width() + 'px',
                    'z-index': 1000
                });
            } else {
                _this.$('.modal-footer').css({
                    'position': 'static'
                });
            }
        },
        show: function (returnHtml) {
            var _this = this;
            if (returnHtml)return _this.html;

            if ($('.' + _this.params.cls).size() == 0) {
                $('body').append(_this.html);
            }
            var top = $(window).height() * 0.4 - _this.$('.modal-dialog').height() * 0.5;
            _this.$('.modal-dialog').css({
                left: ($(window).width() / 2 - _this.$('.modal-dialog').width() / 2) + "px",
                top: dlg_top + 'px'
            });
            if (_this.params.height) {
                _this.$('.modal-content').height($(window).height() * _this.params.height);
            }

            $("#"+_this.id).show();

            if (_this.params.content) {
                _this.setContent(_this.params.content);
            }
        },
        scrollTo: function (params) {
            var _this=this;
            if (params == 'end') {
                $("#"+_this.id).scrollTop(this.$('.modal-dialog').height() + 50);
            }
        },
        scrollToEnd: function () {
            var _this = this;
            _this.scrollTo('end');
        },
        close: function () {
            var _this = this;
            $("#"+_this.id).animate({}, 20, function () {
                if (_this.params.cache) {
                    $("#"+_this.id).hide();
                } else {
                    $("#"+_this.id).remove();
                }
            });
        }
    });
});